package com.veryon9.mobile.common;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;

import com.commonsware.cwac.endless.EndlessAdapter;
import com.veryon9.mobile.common.On9GoldenActivity.ArrayListFragment;
import com.veryon9.mobile.model.SlowForumDataSource;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
import android.support.v4.view.Menu;
import android.support.v4.view.MenuItem;
import android.support.v4.view.Window;
import android.support.v4.view.MenuItem.OnMenuItemClickListener;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ThreadContentActivity extends FragmentActivity {
	 /** Called when the activity is first created. */
	private static int THEME = R.style.Theme_Sherlock;
	
	private static String relativeUrl;
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("Save")
		    .setIcon(R.drawable.ic_compose)
		    .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		
		menu.add("Search")
	        .setIcon(R.drawable.ic_search)
	        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		
		menu.add("Refresh")
	        .setIcon(R.drawable.ic_refresh)
	        .setOnMenuItemClickListener(new OnMenuItemClickListener(){
	        	public boolean onMenuItemClick(MenuItem arg0) {
//	        					arg0.
//	        	                et.setText("Orisun");

	        	                return false;

	        	            }

	        	        })

	        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setTheme(THEME);
		super.onCreate(savedInstanceState);
		Bundle extras = getIntent().getExtras();
		if (extras == null) {
			return;
		}
		relativeUrl = extras.getString("CONTENT_LINK");
		
		requestWindowFeature(Window.FEATURE_ACTION_BAR);
	  if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
            ArrayListFragment list = new ArrayListFragment();
            getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
        }
	  this.setTitle(extras.getString("TITLE"));
	}
	
	public static class ArrayListFragment extends ListFragment {

		SimpleAdapter adapter = null;
		
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            
	  		new LoadItemsTask().execute();
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {

            Log.i("FragmentList", "Item clicked: " + id);
        }
        
    	private class LoadItemsTask extends AsyncTask<Void, Void, Void>
    	{
		   private List<Map<String, String>> items;
		   
		   @Override
		   protected Void doInBackground(Void... params) {
		       	try {
					items = SlowForumDataSource.getPostData(new URL("http://forum9.hkgolden.com/" + relativeUrl));
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		       	return null;
		   }
		   
		   @Override
		   protected void onPostExecute(Void result) {
		       	//adapter.setItems(items);
			   //ForumEndlessAdapter adapter = new ForumEndlessAdapter(items);
			   SimpleAdapter adapter = new SimpleAdapter(ArrayListFragment.this.getActivity(), items, R.layout.post_item, new String[] { "USERNAME", "CONTENT" }, 
   					new int[] {R.id.post_username, R.id.post_content});
			   setListAdapter(adapter);
		   }
        }
    	
//    	private class ForumEndlessAdapter extends EndlessAdapter {
//    		private RotateAnimation rotate=null;
//    		
//    		private List<Map<String, String>> storedItems = null;
//    		private List<Map<String, String>> newItems = null;
//    		
//    		ForumEndlessAdapter(List<Map<String, String>> items) {
//    			super(new SimpleAdapter(ArrayListFragment.this.getActivity(), items, R.layout.listitem, new String[] { "TITLE", "USERNAME", "LINK" }, 
//    					new int[] {R.id.listitem_title, R.id.listitem_username,R.id.listitem_link }));
//    		
//    			storedItems = items;
//    			
//    			rotate=new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
//    																	0.5f, Animation.RELATIVE_TO_SELF,
//    																	0.5f);
//    			rotate.setDuration(600);
//    			rotate.setRepeatMode(Animation.RESTART);
//    			rotate.setRepeatCount(Animation.INFINITE);
//    		}
//    		
//    		@Override
//    		protected View getPendingView(ViewGroup parent) {
//    			View row= getLayoutInflater(null).inflate(R.layout.listitem, null);
//    			
//    			View child=row.findViewById(R.id.listitem_title);
//    			
//    			child.setVisibility(View.GONE);
//    			
//    			child=row.findViewById(R.id.listitem_username);
//    			
//    			child.setVisibility(View.GONE);
//    			
//    			child=row.findViewById(R.id.throbber);
//    			child.setVisibility(View.VISIBLE);
//    			child.startAnimation(rotate);
//    			
//    			return(row);
//    		}
//    		
//    		@Override
//    		protected boolean cacheInBackground() {
//    			//SystemClock.sleep(10000);				// pretend to do work
//    			newItems = SlowForumDataSource.getTopicData();
//    			return(getWrappedAdapter().getCount()<75);
//    		}
//    		
//    		@Override
//    		protected void appendCachedData() {
//    			if (getWrappedAdapter().getCount()<75) {
//    				SimpleAdapter wrap = (SimpleAdapter)getWrappedAdapter();
//    				
//    				for (Map<String, String> cItem : newItems)
//    				{
//    					storedItems.add(cItem);
//    				}
//    				wrap.notifyDataSetChanged();
//    			}
//    		}
//    	}
    	
    	
    }
	
}
